﻿Imports System.Windows.Forms

Public Class frmVerifyTables

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Dim SomeNotFound As Boolean = False
        For Each r As DataGridViewRow In dgTables.Rows
            If Not r.Cells(8).Value Then SomeNotFound = True
        Next
        If SomeNotFound AndAlso WarningMsg("One or more files or tables were missing and were not reselected. Do you want to continue anyway?\n\n\t\b Click 'OK' to ignore these files or tables and drop all associated series\n\t\b Click 'Cancel' to return to the dialog and reselect the files or tables") = Windows.Forms.DialogResult.Cancel Then Exit Sub
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub dgTables_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgTables.CellClick
        With dgTables
            If e.ColumnIndex = .ColumnCount - 1 And e.RowIndex <> -1 Then 'is browse button
                Dim tblType As clsTable.enumTableType = .Item(0, e.RowIndex).Value
                Dim relPath As String = .Item(1, e.RowIndex).Value
                Dim dataSource As String = .Item(2, e.RowIndex).Value
                Dim tblName As String = .Item(3, e.RowIndex).Value
                Dim tblFile As String = .Item(4, e.RowIndex).Value
                Dim projName As String = .Item(5, e.RowIndex).Value
                Dim NewTbl As clsTable
                With New frmSelectTable(dataSource, ExpertMode)
                    .Text = "Select New Table or File"
                    .InitialDatabaseFile = dataSource
                    If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                        NewTbl = .SelectedTables(0)
                    Else
                        Exit Sub
                    End If
                End With
                .Item(0, e.RowIndex).Value = NewTbl.TableType
                .Item(1, e.RowIndex).Value = ""
                .Item(2, e.RowIndex).Value = NewTbl.DataSource
                .Item(3, e.RowIndex).Value = NewTbl.TableName
                .Item(4, e.RowIndex).Value = IIf(NewTbl.IsConvertedDB, IO.Path.GetFileName(NewTbl.OriginalDBName), NewTbl.TableFile)
                .Item(5, e.RowIndex).Value = NewTbl.ProjectName
                Select Case NewTbl.TableType
                    Case clsTable.enumTableType.WRDB5, clsTable.enumTableType.WRDB4
                        .Item(6, e.RowIndex).Value = NewTbl.ProjectName
                        .Item(7, e.RowIndex).Value = NewTbl.TableName
                        .Item(8, e.RowIndex).Value = True
                    Case clsTable.enumTableType.Folder
                        .Item(6, e.RowIndex).Value = IIf(NewTbl.IsConvertedDB, IO.Path.GetDirectoryName(NewTbl.OriginalDBName), NewTbl.DataSource)
                        .Item(2, e.RowIndex).Value = .Item(6, e.RowIndex).Value
                        .Item(7, e.RowIndex).Value = IIf(NewTbl.IsConvertedDB, IO.Path.GetFileName(NewTbl.OriginalDBName), NewTbl.TableFile)
                        .Item(8, e.RowIndex).Value = My.Computer.FileSystem.FileExists(NewTbl.DataSource & NewTbl.TableFile)
                    Case clsTable.enumTableType.Database
                        .Item(6, e.RowIndex).Value = NewTbl.DataSource
                        .Item(7, e.RowIndex).Value = NewTbl.TableFile
                        .Item(8, e.RowIndex).Value = My.Computer.FileSystem.FileExists(NewTbl.DataSource & NewTbl.TableFile)
                        .Item(8, e.RowIndex).Value = My.Computer.FileSystem.FileExists(NewTbl.DataSource)
                    Case clsTable.enumTableType.WRDBData
                        .Item(6, e.RowIndex).Value = NewTbl.DataSource
                        .Item(7, e.RowIndex).Value = "(All Files)"
                        .Item(8, e.RowIndex).Value = My.Computer.FileSystem.DirectoryExists(NewTbl.DataSource)
                    Case clsTable.enumTableType.BMD
                        .Item(6, e.RowIndex).Value = IO.Path.GetDirectoryName(NewTbl.DataSource)
                        .Item(7, e.RowIndex).Value = IO.Path.GetFileName(NewTbl.DataSource)
                        .Item(8, e.RowIndex).Value = My.Computer.FileSystem.FileExists(NewTbl.DataSource)
                End Select
            End If
        End With
    End Sub
End Class
